<?php

namespace app\db\model;

use think\Db;
use think\Model;

class DbUsercareAuth extends Model
{

    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';
    protected $dbName = 'db_usercare_auth';
    protected $resultSetType = 'collection';

    # --------------------------------- 查询类接口 START------------------------------------

    /**
     * 获取医务详情信息
     * @param int $usercare_id 数据库ID
     * @author MSQ 2021-8-2
     **/
    public function getDetails($usercare_id = 0, $field = '*')
    {
        if ($field && $field != '*' && count(explode(',', $field)) == 1) {
            // 只取一个字段
            $info = $this->where(['id' => $usercare_id])->value($field);
        } else {
            $info = db($this->dbName)->field($field)->where(['id' => $usercare_id])->find();
        }

        return $info;
    }
    # ---------------------------------- 查询类接口 END-------------------------------------


    # ------------------------------- 数据处理类接口 START ----------------------------------
    /**
     * 更新医务默认选择的总库 最后一次点击的总库
     * @param int $usercare_id 数据库ID
     * @author MSQ 2021-8-2
     **/
    public function upDefaultSeletDbId($usercare_id = 0, $database_id = 0)
    {
        if (!$usercare_id || !$database_id) return false;
        $this->isUpdate(true)->save(['default_database_show_id' => $database_id], ['id' => $usercare_id]);

        return true;
    }

    # --------------------------------- 数据处理类接口 END ----------------------------------

    /**
     * @Notes:get_auth           获取权限规则
     * @param string $field 字段
     * @param string $order 排序 id desc
     * @param string $type string 且 必须是单一字段
     * @return array|false|string
     * @author:WuXiaoMing
     * Date:2021/8/20
     * Time:4:22 下午
     */
    public function get_auth($field = "*", $order = "", $type = "")
    {
        if ($field && $field != '*' && count(explode(',', $field)) == 1) {
            $ret_info = $this->field($field)->where(['status' => 1])->column($field);
            if ($type == 'string') {
                $ret_info = implode(',', $ret_info);
            }
        } else {
            $ret_info = $this->field($field)->where(['status' => 1])->order($order)->select()->toArray();
        }
        return $ret_info;
    }

}
